package com.example.mypc.fragmentdemo;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by MyPC on 2016/10/11.
 */
public class MyListFragment extends ListFragment {

    /*
    ;列表fragment;
    * */
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setListAdapter(null);
    }


    @Override
    public void setListAdapter(ListAdapter adapter) {
        List<Map<String,String>> map = new ArrayList<>();
        for (int i= 0;i<25;i++){
            Map<String,String> mk = new HashMap<>();
            mk.put("title","这是"+i+"条数据");
            map.add(mk);
        }
        SimpleAdapter ada= new SimpleAdapter(
                getActivity(),
                map,
                android.R.layout.simple_list_item_1,/*listview 单行的布局文件*/
                new String[]{"title"},/*数据map里、对应的空间*/
                new int[]{android.R.id.text1}/*添加数据的控件*/
                );
        super.setListAdapter(ada);
    }

    //点击事件;
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        ((ListTOMainInterface)getActivity()).selected(position);
    }
    //回调接口
    /*我想要activity给我提供一些什么接口与方法*/
    public interface ListTOMainInterface {
        public void selected (int position);
    }

}
